﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace DAL {
   public class ClassRepository {
        public static string Getpk(string brnchid, string orgid) {
            SMSEntities Sms = new SMSEntities();
            var generated = Sms.GetPrimaryKey("Class").ToList();
            return String.Format("CA{0}.{1}-{2:00000000000000}", orgid, brnchid, Convert.ToInt32(generated[0].number));
        }
        //Start the Session 
        public static bool CreateClass(string ClassName, string SessionID, string OrgID, string BranchID) {

            using (TransactionScope transaction = new TransactionScope()) {
                bool IsCreated = false;
                SMSEntities Sms = new SMSEntities();
                Class IClass = new Class();
                IClass.Active = true;
                IClass.BranchID = BranchID;
                IClass.CDate = System.DateTime.Now;
                IClass.OrgID = OrgID;
                IClass.SessionID = SessionID;
                IClass.ClassID = Getpk(BranchID, OrgID);
                IClass.ClassName = ClassName;
                Sms.Classes.Add(IClass);
                Sms.SaveChanges();
                transaction.Complete();
                IsCreated = true;
                return IsCreated;
            }
        }
        //Overload for Create Class
        public static bool CreateClass(string ClassID, string ClassName, string SessionID, string OrgID, string BranchID) {

            using (TransactionScope transaction = new TransactionScope()) {
                bool IsCreated = false;
                SMSEntities Sms = new SMSEntities();
                Class IClass = new Class();
                IClass.Active = true;
                IClass.BranchID = BranchID;
                IClass.CDate = System.DateTime.Now;
                IClass.OrgID = OrgID;
                IClass.SessionID = SessionID;
                IClass.ClassID = ClassID;
                IClass.ClassName = ClassName;
                Sms.Classes.Add(IClass);
                Sms.SaveChanges();
                transaction.Complete();
                IsCreated = true;
                return IsCreated;
            }
        }
        public static List<GetAllClassesActive_Result> GetAllClassActive(string SessionID,string OrgID, string BranchID) {

            SMSEntities Sms = new SMSEntities();
            return Sms.GetAllClassesActive(SessionID,OrgID, BranchID,true).ToList<GetAllClassesActive_Result>();
        }
    }
}
